package com.example.chapp;

import android.content.Intent;
import android.os.Bundle;

import com.example.chapp.control.*;


import com.example.chapp.entity.*;
import com.example.chapp.util.HttpCallback;
import com.example.chapp.util.httputil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;


import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //*********************************
       View but11=findViewById(R.id.but_1);
       but11.setOnClickListener(new View.OnClickListener() {
           Handler mHandler =new Handler(new Handler.Callback() {
               @Override
               public boolean handleMessage(@NonNull Message message) {
                   Gson gson=new Gson();

                       result<user> r = gson.fromJson(message.obj.toString(), new TypeToken<result<user>>() {}.getType());
                     if(r.getCode()==200){
                       user user =r.getData();

                       Toast toast = Toast.makeText(MainActivity.this,"登陆成功!!",Toast.LENGTH_LONG);
                       toast.setGravity(Gravity.CENTER,0,0);
                       toast.show();

                       Intent intent=new Intent(MainActivity.this, shouyeactive.class);
                       startActivity(intent);
                   }else {
                       Toast toast = Toast.makeText(MainActivity.this,"登陆失败!!",Toast.LENGTH_LONG);
                       toast.setGravity(Gravity.CENTER,0,0);
                       toast.show();
                   }
                   return false;
               }
           });
           @Override
           public void onClick(View view) {
               TextView n=findViewById(R.id.admin1);
               TextView p=findViewById(R.id.pwd1);
               httputil.sendRequest("http://192.168.223.1:8080/getuser/"+n.getText()+"/"+p.getText(),
                       "GET",
                       "page=0&&count=25",
                       new HttpCallback() {

                           @Override
                           public void onSuccess(InputStream in) {
                               String result = httputil.decodeAsString(in);
                               Message msg = new Message();
                               msg.what = 1;
                               msg.obj = result;
                               mHandler.sendMessage(msg);
                           }

                           @Override
                           public void onFailure(int code, String mes) {
                               Message msg = new Message();
                               msg.what = 2;
                               msg.obj = mes;
                               mHandler.sendMessage(msg);
                           }

                       });


           }
       });
        View but2=findViewById(R.id.lbut_2);
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,redegitactive.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}